<?php

class Ead_Form_Videoaulas extends Zend_Form
{

    public function init() {
   	    
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        $urlbase = $front->getBaseUrl();
        
        $this->setName('form_'.$controller);
        
        $id = $front->getRequest()->getParam('id');
        if($id > 0){
            $obj = new Ead_Model_DbTable_Videoaulas();
            $data = $obj->fetchRow('VID_ID ='.$id);
        }
        $el = new Zend_Form_Element_Hidden('VID_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $obj = new Ead_Model_DbTable_Catcurso();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r) $arr[$r['CTC_ID']] = $r['CTC_NOM']; 
        /*
        $el = new Zend_Form_Element_Select('CTC_ID',array('onchange' => ''));
        $el->setLabel('Categoria')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty')
        ->addMultiOptions($arr);
        $this->addElement($el);*/
        $arr_set = array();
        if(isset($data['CTC_ID'])){ // 1,2,3,
            try {
                $cat = explode(',', $data['CTC_ID']);
                foreach($cat as $c) $arr_set[$c] = $c;
            } catch(Exception $ex) {
               $arr_set = array($data['CTC_ID']=>$data['CTC_ID']);
            }
        }
        
        $el = new Zend_Form_Element_MultiCheckbox('CTC_IDS');
        $el->setLabel('Categorias')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty')
        ->addMultiOptions($arr)
        ->setValue($arr_set);
        $this->addElement($el);
        

        $el = new Zend_Form_Element_Text('VID_NOM');
        $el->setLabel('Nome')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('VID_PROF');
        $el->setLabel('Professor')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('VID_DUR');
        $el->setLabel('Duração Horas')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('VID_DURMIN');
        $el->setLabel('Duração Minutos')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        /*
        $obj = new Ead_Model_DbTable_Repositorio();
        $row = $obj->fetchAll()->toArray();
        $arr = array(''=>'Nenhum');
        foreach($row as $r) $arr[$r['REP_ID']] = $r['REP_NOM']; 
        
        $el = new Zend_Form_Element_Select('REP_ID',array('onchange' => ''));
        $el->setLabel('Arquivo')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty')
        ->addMultiOptions($arr);
        $this->addElement($el);
        */
        
        $el = new Zend_Form_Element_Hidden('REP_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int')
            ->setAttrib('class', 'ajx_vlr_id find_val')
            ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REP_NOM');
        $el->setLabel('Arquivo')
        ->setDescription('<a href="javascript:void(0)" class="ajx_img clk_sel_rep"></a>')
        ->setDecorators(array('ViewHelper',
        array('Description', array('escape' => false, 'tag' => false)),
        array('HtmlTag', array('tag' => 'dd')),
        array('Label', array('tag' => 'dt')),
        'Errors',))
        ->setRequired(true)
        ->setAttrib('class', 'ajx_vlr_nom find_rep_gl')
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $this->addElement($el);
        
        if($id > 0){
            Zend_Loader::loadClass('imagem','library');
            
            $obj2 = new Ead_Model_DbTable_Videoaulas();
            $row2 = $obj2->fetchRow('VID_ID = '.(int)$id);
            
            $obj = new Ead_Model_DbTable_Repositorio();
            $row = $obj->fetchRow('REP_ID = '.$row2->REP_ID);
            
            $arq = imagem::formatNomeArquivo($row->REP_ID,$row->REP_DATHOR);
 
            $img = 'public/upload/repositorio/'.$arq.'.*';
            foreach (glob($img) as $src) {
                $ext = end(explode('.', $src));    
                if($ext == 'mp4'){
                    
                    $sessao = new Zend_Session_Namespace('Repositorio');
                    $sessao->caminho = '..'.$urlbase.'/public/upload/repositorio/';
                    $sessao->id = $row->REP_ID;
                    $sessao->dathor = $row->REP_DATHOR;
                    
                    Zend_Loader::loadClass('tools','library');
                    
                    $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>tools::frameVideo()));
                    $this->addElement($el);
                }
                else{
                    $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img height="200" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
                    $this->addElement($el);
                }
            }
        }
        
        
        $el = new Zend_Form_Element_Textarea('VID_DESC');
        $el->setLabel('Descrição')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('VID_CENE',array());
        $el->setLabel('Exclusivo para alunos do CENE');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('VID_ORD');
        $el->setLabel('Ordem')
                ->setRequired(true)
                ->addFilter('int')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $btn->setLabel('Gravar');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
        
    }

}

